package com.ztgf.shopping.api.cms.template;


import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.template.TempletModuleBO;
import com.ztgf.shopping.models.bo.template.TempletModuleSaveBO;
import com.ztgf.shopping.models.dto.template.TempletModuleDTO;
import com.ztgf.shopping.models.service.template.ITempletModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 模版功能模块  前端控制器
 * </p>
 *
 * @author YUE
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/cms/templetModule")
@Slf4j
@Api(value = "TempletModuleController",tags = "模版功能模块表接口")
public class TempletModuleController {

    @DubboReference
    private ITempletModuleService iTempletModuleService;

    @ApiOperation(value = "添加模版功能模块",notes = "添加模版功能模块")
    @PostMapping(value = "/insert")
    public Result insert( TempletModuleSaveBO templetModuleSaveBO) {
        if(null == templetModuleSaveBO.getSort())
            return new Result<>(CodeEnum.FAIL_100033);
        boolean flag = iTempletModuleService.add(templetModuleSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存模版功能模块",notes = "保存模版功能模块")
    public Result save( TempletModuleSaveBO templetModuleSaveBO){
        if(templetModuleSaveBO.getId() == null){
            //添加
            return  insert(templetModuleSaveBO);
        } else {
            return  update(templetModuleSaveBO);
        }
    }

    @ApiOperation(value = "修改模版功能模块",notes = "修改模版功能模块")
    @PostMapping("/update")
    public Result update( TempletModuleSaveBO templetModuleSaveBO){
        TempletModuleDTO templetModuleDTO = new TempletModuleDTO();
        BeanUtils.copyProperties(templetModuleSaveBO, templetModuleDTO);
        templetModuleDTO.setId(templetModuleSaveBO.getId());
        boolean flag = iTempletModuleService.updateById(templetModuleDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除模版功能模块",notes = "删除模版功能模块")
    @ApiImplicitParam(name = "id",value = "模版功能模块id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = iTempletModuleService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除模版功能模块",notes = "批量删除模版功能模块")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        iTempletModuleService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<TempletModuleDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        TempletModuleDTO epgMainNavigationDTO = iTempletModuleService.selectById(id);
        return new Result<>(epgMainNavigationDTO);
    }

    @ApiOperation(value = "模版功能模块列表",notes = "模版功能模块列表")
    @PostMapping("/queryList")
    public Result<List<TempletModuleDTO>> queryPageList( TempletModuleBO templetModuleBO){
        List<TempletModuleDTO> templetModuleDTOList = iTempletModuleService.getBySelectTemplatModuleInfoNoPage(templetModuleBO);
        return new Result<>(templetModuleDTOList);
    }
}

